package edu.hcnu.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @description:
 * @author: Administrator
 * @time: 2023/3/1 13:25
 */
@Component
@Aspect
public class LogUtil {

    /**
     *            * edu.hcnu.service.impl.*.*(..)
     *          任意返回值                  这个包下的类的所有方法
     */
    @Pointcut("execution(* edu.hcnu.service.impl.*.*(..))")
    public void pointcut(){}



    // 前置通知
    @Before("pointcut()")
    public static void before(JoinPoint joinPoint) {

        String methodName = joinPoint.getSignature().getName();

        Object[] args = joinPoint.getArgs();


        System.out.println(methodName + "方法运行前，参数是" + ((args == null) ? "" : Arrays.asList(args)));
    }

    //后置通知
    @After("pointcut()")
    public static void after(JoinPoint joinPoint) {

        String methodName = joinPoint.getSignature().getName();

        Object[] args = joinPoint.getArgs();
        System.out.println(methodName + "方法运行后，参数是" + ((args == null) ? "" : Arrays.asList(args)));
    }

    //后置异常通知
    @AfterThrowing(value = "pointcut()", throwing = "ex")
    public static void afterException(JoinPoint joinPoint, Exception ex) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println(methodName + "方法出错了" + ex.getMessage());
    }

    //后置返回通知
    @AfterReturning(value = "pointcut()", returning = "result")
    public static void afterEnd(JoinPoint joinPoint, Object result) {
        String methodName = joinPoint.getSignature().getName();

        System.out.println(methodName + "方法结束 结果是：" + result);
    }
}
